package ar.edu.isft189.pdfBox;

import java.io.IOException;

import javax.swing.JOptionPane;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.helpers.DefaultHandler;

public class ReadXMLFile {
    private DefaultHandler handler = null;
    private SAXParserFactory factory = null;
    private SAXParser saxParser = null;
    private String xmlFile = null;
    
    public ReadXMLFile(final String file) {
        try {
            xmlFile = file;
            factory = SAXParserFactory.newInstance();
            saxParser = factory.newSAXParser();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void setHandler(DefaultHandler h) {
        handler = h;
    }
    
    public DefaultHandler getHandler() {
        return handler;
    }
    
    public void start() {
        if ( handler != null && saxParser != null ) {
            try {
                saxParser.parse(xmlFile, handler);
            }  catch (IOException ioe) {
                JOptionPane.showMessageDialog(null,ioe.getMessage());
                ioe.printStackTrace();
                System.exit(0);
            } catch(Exception e) {
                e.printStackTrace();         
            }
        }
    }
}